package com.java28.rbac.controller;


import com.java28.rbac.pojo.Role;
import com.java28.rbac.pojo.Userrole;
import com.java28.rbac.service.RoleService;
import com.java28.rbac.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("userrole")
public class UserRoleController {

    @Autowired(required = false)
    UserRoleService userRoleService;

    @RequestMapping("assignrole")
    public String assignRoleView(Model model, Integer userid){
        Map<String,Object> map = new HashMap<String,Object>();
        model.addAttribute("userid",userid);
        return "WEB-INF/user/assignRole";
    }

    @RequestMapping("assignRoleList")
    @ResponseBody
    public Map<String,Object> showList(Integer userid){
        Map<String,Object> map = new HashMap<String,Object>();
//        查询该用户未拥有的角色
        List<Role> nullAssignRole = userRoleService.findAllByUserId(userid);
//        查询该用户拥有的角色
        List<Role> isAssignRole = userRoleService.findAllByUserId2(userid);
        try{
            map.put("nullassignrole",nullAssignRole);
            map.put("isassignrole",isAssignRole);
            map.put("result",true);
        }catch(Exception e) {
            map.put("result",false);
        }

        return map;
    }


    @RequestMapping("save")
    @ResponseBody
    public Map<String,Object> save(Integer userid,Integer[] nullrole){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            userRoleService.save(userid,nullrole);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(Integer userid,Integer[] isrole){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            userRoleService.delete(userid,isrole);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }




}


